/*
 * WinLoop.h
 *
 *  Created on: 2012-12-18
 *      Author: yangentao@gmail.com
 */

#pragma once;

#include <Windows.h>
#include <boost/function.hpp>
/*
 int n = 5;
 void onTimer(WinLoop* loop, Timer* t){
 cout<<"Timer"<<endl;
 if(!n--){
 t->stop();
 loop->quit(0);
 }
 }

 static void test() {
 WinLoop loop;
 Timer t;
 t.OnTimer = boost::bind(onTimer, &loop, _1);
 t.start(1000);
 loop.loop();
 }
 */

namespace yet {

class WinLoop {
public:
	WinLoop();
	virtual ~WinLoop();
public:
	virtual int loop();
	void quit(int exitCode = 0);
	bool isLooping();
	bool postMessage(UINT msg, DWORD wParam = 0, DWORD lParam = 0);
public:
	//see TranslateAccelerator
	boost::function<int(MSG*)> OnTranslateAccelator;
	//return 0: process the message in normal way
	//non zero: continue the next message
	boost::function<BOOL(MSG*)> OnPreTranslateMsg;

private:
	DWORD volatile threadID;
	bool volatile looping;
};

} /* namespace yet */
